﻿using System;
using System.Collections.Generic;
using System.IO;
using HK.Editor.BuildSystem;
using UnityEditor;

namespace HK.Editor.Utils
{
    /// <summary>
    /// 工具类 : Build
    /// </summary>
    public static class UtilsBuild
    {
        /// <summary>
        /// 取得所有需要打包的场景列表
        /// </summary>
        /// <returns>所有需要打包的场景列表</returns>
        public static string[] GetAllBuildScenes()
        {
            var scenes = new List<string>();

            foreach (var scene in EditorBuildSettings.scenes)
            {
                if(null == scene || !scene.enabled) continue;
                
                scenes.Add(scene.path);
            }
            
            return 0 >= scenes.Count ? null : scenes.ToArray();
        }
        
        /// <summary>
        /// 生成App文件名
        /// </summary>
        /// <param name="iTarget">打包目标</param>
        /// <returns>App文件名</returns>
        public static string GenerateAppFileName(BuildTarget iTarget)
        {
            var appVersion = PlayerSettings.bundleVersion;
            if (string.IsNullOrEmpty(appVersion))
            {
                appVersion = "0.1.0";
            }
            
            // 时间
            var curDateTime = DateTime.Now;
            var curDateTimeTxt = curDateTime.ToString("yyyyMMddHHmm");
            
            var buildMode = BuildParameters.BuildMode;
            var buildNo = BuildParameters.BuildNo;
            var branch = BuildParameters.Branch;
            var appName = $"{buildMode}";
            if (!string.IsNullOrEmpty(branch))
            {
                branch = branch.Replace("/", "_");
                appName = $"{appName}_{branch}";
            }
            appName = $"{appName}_{appVersion}_{buildNo}_{curDateTimeTxt}";
            
            if (BuildTarget.iOS == iTarget)
            {
                return $"{appName}.ipa";
            }

            if (BuildTarget.Android == iTarget)
            {
                return $"{appName}.apk";
            }

            throw new NotImplementedException($"GenerateAppFileName():Unknown build target!(Target:{iTarget})");
        }
        
        /// <summary>
        /// 输出调试信息信息
        /// </summary>
        /// <param name="iOutputDir">输出目录.</param>
        /// <param name="iDebugInfo">调试信息.</param>
        /// <param name="iClear">清空标识为.</param>
        public static void OutputDebugInfo(string iOutputDir, string iDebugInfo, bool iClear = false) 
        {
            var filePath = $"{iOutputDir}/DebugInfo.log";
            if (iClear && File.Exists (filePath)) {
                File.Delete (filePath);
            }
            using (var fStream = new FileStream (filePath, FileMode.OpenOrCreate, FileAccess.Write))
            {
                // 获得字节数组
                var data = System.Text.Encoding.UTF8.GetBytes(iDebugInfo); 
                // 写入
                fStream.Write (data, 0, data.Length);
            }

        }
    }
}
